package com.app.megaprosoft.modyimcarcare;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;

import com.megaprosoft.modyimcarcare.R;

import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

public class Member extends Activity {
	
	connect con = new connect(); 
	ResultSet rs,rs2,rs3,rs4;
	EditText Tel,PH,PY;
	TextView Bnd,Gen,Pio,Clr,Siz,Grp,regisMem;
	String txtRegis,txtBnd,txtGen,txtPio,txtClr,txtSiz,txtGrp,txtTel,txtPH,txtPY
			,NameInput,company,SearchRegis;
	String ServiceIDS,ServiceType,BrandS,GenerationS,GroupName,id,
    ProvinceS,ColorS,SizeS,carGroupS,PhoneS,HHtimeS,MMtimeS,CarpetS,InsulatorS,mminute,hhour;	
	public  String TypeID,RegisterIDS;
	boolean check = false;
	String OIDInitail;
	AlertDialog.Builder dDialog1;
	ArrayList<String> hh1 = new ArrayList<String>();
	ArrayList<String> mm1 = new ArrayList<String>();
	
	ArrayList<String> hh2 = new ArrayList<String>();
	ArrayList<String> mm2 = new ArrayList<String>();
	
	String timeComeBackHH,timeComeBackMM,timeComeBackAdd;
	int chackTypr; 
	Spinner backtimeMM;
	Spinner backtimeHH;
	
	RadioButton radioWait,radioNoWait;
	RadioGroup radio;


	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_member);
		
		
		
		//final Button sch= (Button) findViewById(R.id.btMember);
		final Button btH = (Button) findViewById(R.id.BTHole);
		final Button btn = (Button) findViewById(R.id.BTNext);
		final TextView nameInput = (TextView) findViewById(R.id.nameInput);
		final TextView CompanyInput = (TextView) findViewById(R.id.Company);
		regisMem = (TextView) findViewById(R.id.regisMem);
		Bnd = (TextView) findViewById(R.id.Brand);
		Gen = (TextView) findViewById(R.id.Generations);
		Pio = (TextView) findViewById(R.id.spn);
		Clr = (TextView) findViewById(R.id.spnColor);
		Siz = (TextView) findViewById(R.id.spnSize);
		Grp = (TextView) findViewById(R.id.CarGroup);
		Tel = (EditText) findViewById(R.id.tel);
		PH = (EditText) findViewById(R.id.carpet);
		PY = (EditText) findViewById(R.id.insulator);
		dDialog1 = new AlertDialog.Builder(this);
		
		Tel.getText().toString();
		
		SearchRegis = getIntent().getExtras().getString("RegisterID");
		NameInput = getIntent().getExtras().getString("name");
		company = getIntent().getExtras().getString("CompanyS");

		nameInput.setText(NameInput);
		CompanyInput.setText(company); 
		
		regisMem.setText(SearchRegis) ;
		
		 backtimeMM = (Spinner) findViewById(R.id.spnTimem);
		 backtimeHH = (Spinner) findViewById(R.id.spnTimeh);
		 backtimeMM.setEnabled(false);
		 backtimeHH.setEnabled(false);
		
		////////////////////////////////////////// Check Member //////////////////////
		

		
		if(check){
			
			
			
			
		}else{
		
		
		String sql = "SELECT * FROM CustomerCar WHERE RegisterID = '"+SearchRegis+"' ORDER BY ServiceID DESC ";  
		
		rs3 = con.selectData(sql);
		try {
			rs3.next();
			
			txtRegis = rs3.getString("RegisterID");
			txtBnd = rs3.getString("Brand");
			txtGen = rs3.getString("Generation");
			txtPio = rs3.getString("Province");
			txtClr = rs3.getString("Color");
			txtSiz = rs3.getString("Size");
			txtGrp = rs3.getString("CarGroup");
			txtTel = rs3.getString("Phone");
			txtPH = rs3.getString("Carpet");
			txtPY = rs3.getString("Insulator");
			
		} catch (SQLException e1) {
			
			e1.printStackTrace();
		}
		
		
		if(SearchRegis.equals(txtRegis)){
			
			String sqlOIDgroupName = "SELECT * FROM CarGroup WHERE OID = '"+txtGrp+"' ";  
			
			rs4 = con.selectData(sqlOIDgroupName);
			try {
				rs4.next();
				
				txtGrp = rs4.getString("CarGroupName");
				OIDInitail = rs4.getString("OID");
				
				
			} catch (SQLException e1) {
				
				e1.printStackTrace();
			}
		
		Bnd.setText(txtBnd);
		Gen.setText(txtGen);
		Pio.setText(txtPio);
		Clr.setText(txtClr);
		Siz.setText(txtSiz);
		Grp.setText(txtGrp);
		Tel.setText(txtTel);
		PH.setText(txtPH);
		PY.setText(txtPY);
		
		}else{
			Toast.makeText(Member.this, "ö����¹"+SearchRegis+"���������Ҫԡ", Toast.LENGTH_LONG).show();
			finish();
			
			
		}
			} 
	
		
		      
		
		////////////////////// ���� ��Ҫԡ �ҡ �Ţ����¹ö ////////////////////////////
		//Search();
		timeHour();
		timeMinute();
		waitForComeBack();
		//////////////////////////////////////////////////////
		
			
		//////////////////////------- gen ID --------//////////////////////////
///////////////////////////////////////////////////////////////	
///////////////////////////////////////////////////////////////	
		
				
		Calendar c = Calendar.getInstance();
		SimpleDateFormat df = new SimpleDateFormat("HHmmss");
		String formattedDate = df.format(c.getTime());
		
		//a = ;
		
		
		try {
		id = generateBillNo()+formattedDate;
		} catch (Exception e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
		}

		
		
		
		// Button go to wash
		
		
				btn.setOnClickListener(new View.OnClickListener() {
					public void onClick(View v) {
						
						
						
						
						rs2 = con.selectData("SELECT * FROM ServiceType WHERE TypeID = '1' ");
						try {
							rs2.next();
							TypeID = rs2.getString("TypeID");
							ServiceType = rs2.getString("TypeName");
						} catch (SQLException e1) {
							// TODO Auto-generated catch block
							e1.printStackTrace();
						}
						
						PhoneS =Tel.getText().toString();

						if(PhoneS.equals(null)||PhoneS.equals("")){
			            	
			            	dDialog1.setTitle("�ѧ������͡�������վ�� ");
			            	dDialog1.setIcon(android.R.drawable.btn_star_big_on); 
			            	dDialog1.setMessage("��سҡ�͡�������Ѿ�� ��������ͧ������  '-'");
			            	dDialog1.setPositiveButton("�Դ", null);
			            	dDialog1.show();
			       

			}else{
						
						 String sql = "INSERT INTO CustomerCar " +
									"(ServiceID,ServiceType,Company,Brand,Generation,RegisterID,Province,Color,Size,CarGroup,Phone,Hour,Minute,Carpet,Insulator,CreatedDate,timeComeBack) " +
									"VALUES (" +
									"'"+id+"'" + ',' + 
									"'"+ServiceType+"'" + ',' + 									
									"'"+company+"'" + ',' +
									"'"+txtBnd+"'" + ',' + 
									"'"+txtGen+"'" + ',' +
									"'"+txtRegis+"'" + ',' + 
									"'"+txtPio+"'" + ',' +
									"'"+txtClr+"'" + ',' +
									"'"+txtSiz+"'" + ',' +
									"'"+OIDInitail+"'" + ',' +
									"'"+PhoneS+"'" + ',' +
									"'"+hhour+"'" + ',' +
									"'"+mminute+"'" + ',' +
									"'"+PH.getText().toString()+"'" + ',' +
									"'"+PY.getText().toString()+"'" + ',' +
									" GETDATE() " + ',' +
							        "'"+timeComeBackAdd+"'"+" ) "; 
						
						if(con.addData(sql)){	 
						Toast.makeText(Member.this, "Inserted data successfully", Toast.LENGTH_SHORT).show();
						Intent newActivityAdd = new Intent(Member.this,services.class);
						newActivityAdd.putExtra("HH", hhour);
						newActivityAdd.putExtra("MM", mminute);
						newActivityAdd.putExtra("name", NameInput);
						newActivityAdd.putExtra("CompanyS", company);
						newActivityAdd.putExtra("OID", OIDInitail);
						newActivityAdd.putExtra("TypeID", TypeID);
						newActivityAdd.putExtra("RegisterIDS", txtRegis);
						newActivityAdd.putExtra("addSer", "");
						startActivity(newActivityAdd);
						}else{
					    Toast.makeText(Member.this, "Inserted data fail"
					    		, Toast.LENGTH_SHORT).show();
					    String ccc = id;
						
						Toast.makeText(Member.this, ccc, Toast.LENGTH_LONG).show();
						}
						
						 
					}
					}
					});
				
				// Button go to hole
				
				
				btH.setOnClickListener(new View.OnClickListener() {
					public void onClick(View v) {
						
						
						rs2 = con.selectData("SELECT * FROM ServiceType WHERE TypeID = '2' ");
						try {
							rs2.next();
							TypeID = rs2.getString("TypeID");
							ServiceType = rs2.getString("TypeName");
						} catch (SQLException e1) {
							// TODO Auto-generated catch block
							e1.printStackTrace();
						}
						
						PhoneS = Tel.getText().toString();
						
						if(PhoneS.equals(null)||PhoneS.equals("")){
			            	
			            	dDialog1.setTitle("�ѧ������͡�������վ�� ");
			            	dDialog1.setIcon(android.R.drawable.btn_star_big_on); 
			            	dDialog1.setMessage("��سҡ�͡�������Ѿ�� ��������ͧ������  '-'");
			            	dDialog1.setPositiveButton("�Դ", null);
			            	dDialog1.show();
			       

							}else{
					
						 String sql = "INSERT INTO CustomerCar " +
									"(ServiceID,ServiceType,Company,Brand,Generation,RegisterID,Province,Color,Size,CarGroup,Phone,Hour,Minute,Carpet,Insulator,CreatedDate,timeComeBack) " +
									"VALUES (" +
									"'"+id+"'" + ',' + 
									"'"+ServiceType+"'" + ',' + 									
									"'"+company+"'" + ',' +
									"'"+txtBnd+"'" + ',' + 
									"'"+txtGen+"'" + ',' +
									"'"+txtRegis+"'" + ',' + 
									"'"+txtPio+"'" + ',' +
									"'"+txtClr+"'" + ',' +
									"'"+txtSiz+"'" + ',' +
									"'"+OIDInitail+"'" + ',' +
									"'"+PhoneS+"'" + ',' +
									"'"+hhour+"'" + ',' +
									"'"+mminute+"'" + ',' +
									"'"+PH.getText().toString()+"'" + ',' +
									"'"+PY.getText().toString()+"'" + ',' +
									" GETDATE() " + ',' +
							        "'"+timeComeBackAdd+"'"+" ) "; 

						if(con.addData(sql)){	 
						Toast.makeText(Member.this, "Inserted data successfully", Toast.LENGTH_SHORT).show();	
						Intent newActivityAdd = new Intent(Member.this,HoleActivity.class);
						newActivityAdd.putExtra("HH", hhour);
						newActivityAdd.putExtra("MM", mminute);
						newActivityAdd.putExtra("name", NameInput);
						newActivityAdd.putExtra("CompanyS", company);
						newActivityAdd.putExtra("OID", OIDInitail);
						newActivityAdd.putExtra("TypeID", TypeID);
						newActivityAdd.putExtra("RegisterIDS", txtRegis);
						newActivityAdd.putExtra("addHole", "");
						startActivity(newActivityAdd);
						}else{
					    Toast.makeText(Member.this, "Inserted data fail", Toast.LENGTH_SHORT).show();
					    String ccc = id;
						
						Toast.makeText(Member.this, ccc, Toast.LENGTH_LONG).show();
						}
						
					}
					}
					});
/*
				////////////////////////go home
				btHome.setOnClickListener(new View.OnClickListener() {
					public void onClick(View v) {
						
						Intent home = new Intent(InitialServices.this,MainActivity.class);
						home.putExtra("name", NameInput);
						home.putExtra("CompanyS", company);
						startActivity(home);
						
					}
					});
*/
		
	}
	

private void timeHour() {
	
	final Spinner timeHH = (Spinner) findViewById(R.id.spnHour);
	
			hh1.add("00");
			hh1.add("01");
			hh1.add("02");
			hh1.add("03");
			hh1.add("04");
			hh1.add("05");
			hh1.add("06");		
			hh1.add("07");
			hh1.add("08");
			hh1.add("09");
			hh1.add("10");
			hh1.add("11");
			hh1.add("12");
			hh1.add("13");
			hh1.add("14");
			hh1.add("15");
			hh1.add("16");
			hh1.add("17");
			hh1.add("18");
			hh1.add("19");
			hh1.add("20");
			hh1.add("21");
			hh1.add("22");
			hh1.add("23");		
			
			
	ArrayAdapter<String> empolyeeAdapter = new ArrayAdapter<String>

(this,android.R.layout.simple_spinner_item,hh1);
	empolyeeAdapter.setDropDownViewResource

(android.R.layout.simple_dropdown_item_1line);
	timeHH.setAdapter(empolyeeAdapter);
	
	timeHH.setOnItemSelectedListener(new  

AdapterView.OnItemSelectedListener() { 

        public void onItemSelected(AdapterView<?> adapterView, 
      	View view, int i, long l) { 
      	// TODO Auto-generated method stub
        	
        	//carGroup = strSpn.get(i).get("expCard"); 
        	hhour = hh1.get(i);
        	
        	}
        public void onNothingSelected(AdapterView<?> arg0) {
			// TODO Auto-generated method stub
			Toast.makeText(Member.this,
     				String.valueOf("Your Selected Empty"),
     					

Toast.LENGTH_SHORT).show();
		} 

    });
	
	}	
////////////////////////////////////////////////////////////////////////////////////////////
private void timeMinute() {
	final Spinner timeMM = (Spinner) findViewById(R.id.spnMinute);
	
	
			
			mm1.add("00");
			mm1.add("01");
			mm1.add("02");
			mm1.add("03");
			mm1.add("04");
			mm1.add("05");
			mm1.add("06");		
			mm1.add("07");
			mm1.add("08");
			mm1.add("09");
			mm1.add("10");
			mm1.add("11");
			mm1.add("12");
			mm1.add("13");
			mm1.add("14");
			mm1.add("15");
			mm1.add("16");
			mm1.add("17");
			mm1.add("18");
			mm1.add("19");
			mm1.add("20");
			mm1.add("21");
			mm1.add("22");
			mm1.add("23");		
			mm1.add("24");
			mm1.add("25");
			mm1.add("26");
			mm1.add("27");
			mm1.add("28");
			mm1.add("29");
			mm1.add("30");
			mm1.add("31");
			mm1.add("32");
			mm1.add("33");
			mm1.add("34");
			mm1.add("35");
			mm1.add("36");
			mm1.add("37");
			mm1.add("38");
			mm1.add("39");
			mm1.add("40");
			mm1.add("41");
			mm1.add("42");
			mm1.add("43");
			mm1.add("44");		
			mm1.add("45");
			mm1.add("46");
			mm1.add("47");
			mm1.add("48");
			mm1.add("49");
			mm1.add("50");
			mm1.add("51");
			mm1.add("52");
			mm1.add("53");
			mm1.add("54");
			mm1.add("55");
			mm1.add("56");
			mm1.add("57");
			mm1.add("58");
			mm1.add("59");
			
			
	ArrayAdapter<String> empolyeeAdapter = new ArrayAdapter<String>

(this,android.R.layout.simple_spinner_item,mm1);
	empolyeeAdapter.setDropDownViewResource

(android.R.layout.simple_dropdown_item_1line);
	timeMM.setAdapter(empolyeeAdapter);
	
	timeMM.setOnItemSelectedListener(new  

AdapterView.OnItemSelectedListener() { 

        public void onItemSelected(AdapterView<?> adapterView, 
      	View view, int i, long l) { 
      	// TODO Auto-generated method stub
        	
        	//carGroup = strSpn.get(i).get("expCard"); 
        	mminute = mm1.get(i);
        	
        	}
        public void onNothingSelected(AdapterView<?> arg0) {
			// TODO Auto-generated method stub
			Toast.makeText(Member.this,
     				String.valueOf("Your Selected Empty"),
     					

Toast.LENGTH_SHORT).show();
		} 

    });
	
	}

public String generateBillNo() throws Exception{

    
    String StrSql = "SELECT COUNT(ServiceID)+1," +
    		  "MONTH(GETDATE()), "+
   		  "CONVERT(VARCHAR(2), GETDATE(), 11) "+
            "FROM CustomerCar "+
            "WHERE CONVERT(VARCHAR(7),[CreatedDate], 111) = CONVERT(VARCHAR(7), GETDATE(), 111) ";


			rs = con.selectData(StrSql);
			
			String No = "";
				
			if(rs.next()){
				if(rs.getInt(1) < 10)
					No = "00"+rs.getInt(1);
				else if(rs.getInt(1) < 100)
					No = "0"+rs.getInt(1);
				else
					No = ""+rs.getInt(1);
			
		
				return ""+rs.getInt(3)+""+rs.getInt(2)+No;	// Prefix + YYMMDD + XXXX
				
			}else{
				
				return null;
				//return null;
			}
}



private void backtimeHour() {
	
	
			hh2.add("00");
			hh2.add("01");
			hh2.add("02");
			hh2.add("03");
			hh2.add("04");
			hh2.add("05");
			hh2.add("06");		
			hh2.add("07");
			hh2.add("08");
			hh2.add("09");
			hh2.add("10");
			hh2.add("11");
			hh2.add("12");
			hh2.add("13");
			hh2.add("14");
			hh2.add("15");
			hh2.add("16");
			hh2.add("17");
			hh2.add("18");
			hh2.add("19");
			hh2.add("20");
			hh2.add("21");
			hh2.add("22");
			hh2.add("23");		
			
			ArrayAdapter<String> empolyeeAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,hh2);
			empolyeeAdapter.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
			backtimeHH.setAdapter(empolyeeAdapter);
			
			backtimeHH.setOnItemSelectedListener(new  AdapterView.OnItemSelectedListener() { 

		        public void onItemSelected(AdapterView<?> adapterView, 
		      	View view, int i, long l) { 
		      	// TODO Auto-generated method stub
		        	timeComeBackHH = hh2.get(i);
		        	timeComeBackAdd = timeComeBackHH + ":" + timeComeBackMM;
		      	
		        	}
		        public void onNothingSelected(AdapterView<?> arg0) {
					// TODO Auto-generated method stub
					Toast.makeText(Member.this,
		     				String.valueOf("Your Selected Empty"),
		     					Toast.LENGTH_SHORT).show();
				}
		        
		});
	
	}	



private void backtimeMinute() {
	
	
	
		
			mm2.add("00");
			mm2.add("01");
			mm2.add("02");
			mm2.add("03");
			mm2.add("04");
			mm2.add("05");
			mm2.add("06");		
			mm2.add("07");
			mm2.add("08");
			mm2.add("09");
			mm2.add("10");
			mm2.add("11");
			mm2.add("12");
			mm2.add("13");
			mm2.add("14");
			mm2.add("15");
			mm2.add("16");
			mm2.add("17");
			mm2.add("18");
			mm2.add("19");
			mm2.add("20");
			mm2.add("21");
			mm2.add("22");
			mm2.add("23");		
			mm2.add("24");
			mm2.add("25");
			mm2.add("26");
			mm2.add("27");
			mm2.add("28");
			mm2.add("29");
			mm2.add("30");
			mm2.add("31");
			mm2.add("32");
			mm2.add("33");
			mm2.add("34");
			mm2.add("35");
			mm2.add("36");
			mm2.add("37");
			mm2.add("38");
			mm2.add("39");
			mm2.add("40");
			mm2.add("41");
			mm2.add("42");
			mm2.add("43");
			mm2.add("44");		
			mm2.add("45");
			mm2.add("46");
			mm2.add("47");
			mm2.add("48");
			mm2.add("49");
			mm2.add("50");
			mm2.add("51");
			mm2.add("52");
			mm2.add("53");
			mm2.add("54");
			mm2.add("55");
			mm2.add("56");
			mm2.add("57");
			mm2.add("58");
			mm2.add("59");
			
		

	ArrayAdapter<String> empolyeeAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,mm2);
	empolyeeAdapter.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
	backtimeMM.setAdapter(empolyeeAdapter);
	
	backtimeMM.setOnItemSelectedListener(new  AdapterView.OnItemSelectedListener() { 

        public void onItemSelected(AdapterView<?> adapterView, 
      	View view, int i, long l) { 
      	// TODO Auto-generated method stub
        	timeComeBackMM = mm2.get(i);

        	timeComeBackAdd = timeComeBackHH + ":" + timeComeBackMM;
        	}
        public void onNothingSelected(AdapterView<?> arg0) {
			// TODO Auto-generated method stub
			Toast.makeText(Member.this,
     				String.valueOf("Your Selected Empty"),
     					Toast.LENGTH_SHORT).show();
		}
        
});



}	


public void waitForComeBack(){
	
	 radio= (RadioGroup)findViewById(R.id.wanttowait);
	 radioWait = (RadioButton)findViewById (R.id.wait);
	 radioNoWait = (RadioButton)findViewById (R.id.nowait);
	 


	 radio.setOnCheckedChangeListener ( new RadioGroup.OnCheckedChangeListener ( )
	 {
	 public void onCheckedChanged ( RadioGroup group, int checkedId )
	 {
	 RadioButton checkedRadio = (RadioButton) Member.this.findViewById (checkedId);
	 chackTypr = checkedRadio.getId(); 
	 
	   if(chackTypr == R.id.wait){
		   
		   backtimeHour();
		   backtimeMinute();
		   backtimeMM.setEnabled(true);
		   backtimeHH.setEnabled(true);
  
	
	   } 
	   else if (chackTypr == R.id.nowait) {
          
		   backtimeMM = (Spinner) findViewById(R.id.spnTimem);
			 backtimeHH = (Spinner) findViewById(R.id.spnTimeh);
			 ArrayList<String> hh2 = new ArrayList<String>();
			ArrayList<String> mm2 = new ArrayList<String>();
			 
			ArrayAdapter<String> empolyeeAdapter = new ArrayAdapter<String>(Member.this,android.R.layout.simple_spinner_item,hh2);
			empolyeeAdapter.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
			backtimeHH.setAdapter(empolyeeAdapter);
			
			ArrayAdapter<String> empolyeeAdapter2 = new ArrayAdapter<String>(Member.this,android.R.layout.simple_spinner_item,mm2);
			empolyeeAdapter2.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
			backtimeMM.setAdapter(empolyeeAdapter2);
		   
		   backtimeMM.setSelected(false);
		   backtimeHH.setSelected(false);
		   backtimeMM.setEnabled(false);
		   backtimeHH.setEnabled(false);
		   timeComeBackAdd();
		  
	        } 

	   }
	 });
	
}

public void timeComeBackAdd() {
	
	timeComeBackAdd = null;
	
	  
}




}
